<?php
$ignore_properties = array ("table_name", "primary_key_value", "primary_key_field" );

/**
 * Formats the field_name into Camel Case (with spaces)
 * @param string $field_name
 * @return string The formatted field name
 */
function formatFieldNameToTitle($field_name) {
	//replace underscores with spaces
	$formattedFieldName = str_ireplace ( "_", " ", $field_name );
	//upper case the first letter of each word
	$formattedFieldName = ucwords ( $formattedFieldName );
	
	return $formattedFieldName;
}

/**
 * Renders just a table row of the titles
 */
function renderPropertiesInputsTitles($class, $model_properties, $ignore_properties) {
	?><tr>
	<?php
	foreach ( $model_properties as $model_property => $value ) {
		if (in_array ( $model_property, $ignore_properties )) {
			continue;
		} else if (is_array ( $value )) {
			//this is a foreign key array, ignore it
			continue;
		} else {
			?><th><?php
			echo $model_property;
			?></th><?php
		}
	}
	?>
</tr><?php
}

/**
 * Renders the model properties inputs,
 *
 * @param array $model_properties
 */
function renderPropertiesInputsRows($class, $model_properties, $ignore_properties) {
	?>
<tr>
	<?php
	foreach ( $model_properties as $model_property => $value ) {
		
		//TODO: check if it's the primary key => make it display somewhere small and uneditable
		//TODO: check if it's a timestamp => show with the primary key and uneditable
		//TODO: check if it's a boolean => make it a checkbox
		//TODO: check if it's an int => make it an input that accepts only ints
		//TODO: check if it's a text or large varchar => make it a textarea
		//TODO: check if it's a regular size varchar => make it a text input
		//TODO: check if it's a datetime, make it a date picker object
		//TODO: check if it's a foreign key pointer (e.g. act_parent_id) => make it a pulldown
		//TODO: show tab objects with editable properties and Edit link
		//TODO: save button starts disabled
		//TODO: save all on editable properties, on save, check which ones have changed, save those
		//TODO: on edit of any property, enable save button and change color of Save button to alert color
		//TODO: check if it's a foreign key array => make it a tab
		

		if (in_array ( $model_property, $ignore_properties )) {
			continue;
		} else if (is_array ( $value )) {
			//this is a foreign key array, ignore it
			continue;
		}
		
		$get_field_properties = "get_" . $model_property . "_properties";
		
		/* @var $field_properties FieldProperties */
		$field_properties = $class->$get_field_properties ();
		if ($field_properties->index == PRIMARY_FIELD || $field_properties->index == MULTIPLE_VALUE_INDEX_FIELD) {
			?><td><?php
			echo $value?></td><?php
		} else if ($field_properties->data_type == "DateTime") {
			?><td><?php
			echo $value?></td><?php
		} else if ($field_properties->data_type == "boolean") {
			?><td><input type="checkbox" <?php
			if ($value) {
				?>
		checked="checked" <?php
			}
			?>
	/></td>
			<?php
		} else if ($field_properties->data_type == "TextArea") {
			?><td><?php
			?><textarea
		title="<?php
			echo $field_properties->comments;
			?>"
	><?php
			echo $value;
			?></textarea></td><?php
		} else if ($field_properties->data_type == "float") {
			?><td><?php
			?>
<input type="text" size="3" value="<?php
			echo $value;
			?>"
		title="<?php
			echo $field_properties->comments;
			?>"
	/></td>
<?php
		} else if ($field_properties->data_type == "integer") {
			?><td><?php
			?>
<input type="text" size="1" value="<?php
			echo $value;
			?>"
		title="<?php
			echo $field_properties->comments;
			?>"
	/></td>
<?php
		} else {
			?><td><?php
			?>
<input type="text" value="<?php
			echo $value;
			?>"
		title="<?php
			echo $field_properties->comments;
			?>"
	/></td>
<?php
		}
	}
	?>
	</tr>
<tr>
	<td><a href="#"
		onclick="
			openClassView('<?php
	echo get_class ( $class );
	?>', '<?php
	echo $class->primary_key_value;
	?>')
			"
	>Edit</a></td>
	<td><input type="button" value="Save" disabled="disabled" /></td>
</tr>
<?php
}

/**
 * Gets the array of foreign keys
 *
 * @param array $model_properties
 * @return array $foreign_key_arrays
 */
function getForeignKeyArray($model_properties) {
	$foreign_key_arrays = array ();
	foreach ( $model_properties as $model_property => $value ) {
		if (is_array ( $value )) {
			array_push ( $foreign_key_arrays, $model_property );
		}
	}
	return $foreign_key_arrays;
}

?>